package com.ruoyi.ftrl.service.impl;

import com.ruoyi.ftrl.mapper.EquipmentDockingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ftrl.mapper.AvgTemperatureMapper;
import com.ruoyi.ftrl.domain.AvgTemperature;
import com.ruoyi.ftrl.service.IAvgTemperatureService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AvgTemperatureServiceImpl implements IAvgTemperatureService {
    @Autowired
    private AvgTemperatureMapper avgTemperatureMapper;
    @Autowired
    private EquipmentDockingMapper equipmentDockingMapper;

    @Override
    public List<AvgTemperature> selectAvgTemperatureList(AvgTemperature avgTemperature) {
        return avgTemperatureMapper.selectAvgTemperatureList(avgTemperature);
    }

    @Override
    public AvgTemperature selectAvgTemperatureById(Long id) {
        return avgTemperatureMapper.selectAvgTemperatureById(id);
    }

    @Override
    public int insertAvgTemperature(AvgTemperature avgTemperature) {
        return avgTemperatureMapper.insertAvgTemperature(avgTemperature);
    }

    @Override
    public int updateAvgTemperature(AvgTemperature avgTemperature) {
        return avgTemperatureMapper.updateAvgTemperature(avgTemperature);
    }

    @Override
    public int deleteAvgTemperatureByIds(Long[] ids) {
        return avgTemperatureMapper.deleteAvgTemperatureByIds(ids);
    }

    @Override
    public int deleteAvgTemperatureById(Long id) {
        return avgTemperatureMapper.deleteAvgTemperatureById(id);
    }

    /**
     * 定时更新小区平均温度
     * @return
     */
    @Override
    public List<AvgTemperature> selectAvgTemperatureAllList() {
        List<AvgTemperature> avgTemperatures = avgTemperatureMapper.selectAvgTemperatureList(new AvgTemperature());
        //获取想要的小区名称数据
        List<String> names = avgTemperatures.stream().map(AvgTemperature::getName).toList();
        //从室温数据记录表中获取想要的小区的平均室内温度
        for (String name : names) {
            //小区的平均室内温度
            Double temp = equipmentDockingMapper.selectAvgTemperatureByName(name);
            if (temp != null) {
                //将最新的小区平均室温数据进行
            avgTemperatureMapper.updateAvgTemperatureByName(name,temp);
            }else {
                //若该小区没有相关的室温采集器
                avgTemperatureMapper.updateAvgTemperatureRemark(name);
            }

        }

        return avgTemperatures;
    }
}
